home *** CD-ROM | disk | FTP | other *** search
/ Czech Logic, Card & Gambling Games / Logické hry.iso / hry / Robocode / robocode-setup-1.0.7.jar / extract.jar / robots / sample / RamFire.java < prev    next >
Encoding:
Java Source  |  2005-02-18  |  1.3 KB  |  61 lines

  1. package sample;
  2. import robocode.*;
  3. /**
  4.  * RamFire - a sample robot by Mathew Nelson
  5.  * 
  6.  * Drives at robots trying to ram them.
  7.  * Fires when it hits them.
  8.  */
  9. public class RamFire extends Robot
  10. {
  11.     int turnDirection = 1;    // Clockwise or counterclockwise
  12.     /**
  13.      * run: Spin around looking for a target
  14.      */
  15.     public void run() {
  16.         while (true)
  17.         {
  18.             turnRight(5 * turnDirection);
  19.         }
  20.     }
  21.     
  22.     /**
  23.      * onScannedRobot:  We have a target.  Go get it.
  24.      */
  25.     public void onScannedRobot(ScannedRobotEvent e) {
  26.                 
  27.         if (e.getBearing() >= 0)
  28.             turnDirection = 1;
  29.         else
  30.             turnDirection = -1;
  31.             
  32.         turnRight(e.getBearing());
  33.         ahead(e.getDistance() + 5);
  34.         scan(); // Might want to move ahead again!
  35.     }
  36.     
  37.     /**
  38.      * onHitRobot:  Turn to face robot, fire hard, and ram him again!
  39.      */
  40.     public void onHitRobot(HitRobotEvent e) {
  41.         if (e.getBearing() >= 0)
  42.             turnDirection = 1;
  43.         else
  44.             turnDirection = -1;
  45.         turnRight(e.getBearing());
  46.         
  47.         // Determine a shot that won't kill the robot...
  48.         // We want to ram him instead for bonus points
  49.         if (e.getEnergy() > 16)
  50.             fire(3);
  51.         else if (e.getEnergy() > 10)
  52.             fire(2);
  53.         else if (e.getEnergy() > 4)
  54.             fire(1);
  55.         else if (e.getEnergy() > 2)
  56.             fire(.5);
  57.         else if (e.getEnergy() > .4)
  58.             fire(.1);
  59.         ahead(40); // Ram him again!
  60.     }
  61. }